package com.ruoyi.basebill.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basebill.DTO.GjjjtEntryInfo;
import com.ruoyi.basebill.DTO.GjjjtInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.WfzGjjjtentry;
import com.ruoyi.basebill.mapper.WfzGjjjtMapper;
import com.ruoyi.basebill.domain.WfzGjjjt;
import com.ruoyi.basebill.service.IWfzGjjjtService;

/**
 * 公积金计提Service业务层处理
 * 
 * @author xiefeng
 * @date 2022-12-14
 */
@Service
public class WfzGjjjtServiceImpl implements IWfzGjjjtService 
{
    @Autowired
    private WfzGjjjtMapper wfzGjjjtMapper;

    /**
     * 查询公积金计提
     * 
     * @param fid 公积金计提主键
     * @return 公积金计提
     */
    @Override
    public WfzGjjjt selectWfzGjjjtByFid(Long fid)
    {
        return wfzGjjjtMapper.selectWfzGjjjtByFid(fid);
    }

    /**
     * 查询公积金计提列表
     * 
     * @param wfzGjjjt 公积金计提
     * @return 公积金计提
     */
    @Override
    public List<WfzGjjjt> selectWfzGjjjtList(WfzGjjjt wfzGjjjt)
    {
        return wfzGjjjtMapper.selectWfzGjjjtList(wfzGjjjt);
    }

    /**
     * 新增公积金计提
     * 
     * @param wfzGjjjt 公积金计提
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWfzGjjjt(WfzGjjjt wfzGjjjt)
    {
        int rows = wfzGjjjtMapper.insertWfzGjjjt(wfzGjjjt);
        insertWfzGjjjtentry(wfzGjjjt);
        return rows;
    }

    /**
     * 修改公积金计提
     * 
     * @param wfzGjjjt 公积金计提
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWfzGjjjt(WfzGjjjt wfzGjjjt)
    {
        wfzGjjjtMapper.deleteWfzGjjjtentryByFparentid(wfzGjjjt.getFid());
        insertWfzGjjjtentry(wfzGjjjt);
        return wfzGjjjtMapper.updateWfzGjjjt(wfzGjjjt);
    }

    /**
     * 批量删除公积金计提
     * 
     * @param fids 需要删除的公积金计提主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzGjjjtByFids(Long[] fids)
    {
        wfzGjjjtMapper.deleteWfzGjjjtentryByFparentids(fids);
        return wfzGjjjtMapper.deleteWfzGjjjtByFids(fids);
    }

    /**
     * 删除公积金计提信息
     * 
     * @param fid 公积金计提主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzGjjjtByFid(Long fid)
    {
        wfzGjjjtMapper.deleteWfzGjjjtentryByFparentid(fid);
        return wfzGjjjtMapper.deleteWfzGjjjtByFid(fid);
    }

    /**
     * 新增公积金计提分录信息
     * 
     * @param wfzGjjjt 公积金计提对象
     */
    public void insertWfzGjjjtentry(WfzGjjjt wfzGjjjt)
    {
        List<WfzGjjjtentry> wfzGjjjtentryList = wfzGjjjt.getWfzGjjjtentryList();
        Long fid = wfzGjjjt.getFid();
        if (StringUtils.isNotNull(wfzGjjjtentryList))
        {
            List<WfzGjjjtentry> list = new ArrayList<WfzGjjjtentry>();
            for (WfzGjjjtentry wfzGjjjtentry : wfzGjjjtentryList)
            {
                wfzGjjjtentry.setFparentid(fid);
                list.add(wfzGjjjtentry);
            }
            if (list.size() > 0)
            {
                wfzGjjjtMapper.batchWfzGjjjtentry(list);
            }
        }
    }

    /**
     * 导入公积金计提
     */
    @Override
    public String importGjjjt(WfzGjjjt wfzGjjjt) {
        GjjjtInfo gjjjtInfo = new GjjjtInfo();
        String fnumber = wfzGjjjt.getFnumber();//单据编号
        String fcompany = wfzGjjjt.getFcompany();//公司（组织）
        String fperiod = wfzGjjjt.getFperiod();//薪酬期间
        String fvourtype = wfzGjjjt.getFvourtype();//凭证字

        gjjjtInfo.setNumber(fnumber);
        gjjjtInfo.setCompany(fcompany);
        gjjjtInfo.setPeriod(fperiod);
        gjjjtInfo.setVourType(fvourtype);
        gjjjtInfo.setSourcebillid(String.valueOf(wfzGjjjt.getFid()));

        List<WfzGjjjtentry> wfzGjjjtentryList = wfzGjjjt.getWfzGjjjtentryList();
        ArrayList<GjjjtEntryInfo> entry = new ArrayList<>();
        //组装分录信息
        for (int i = 0; i < wfzGjjjtentryList.size(); i++) {
            WfzGjjjtentry wfzGjjjtentry = wfzGjjjtentryList.get(i);
            String fcostnumber = wfzGjjjtentry.getFcostnumber();//成本中心
            String fcostcompany = wfzGjjjtentry.getFcostcompany();//成本中心所属财务组织
            BigDecimal fgjj = wfzGjjjtentry.getFgjj();//公积金
            BigDecimal fgrgjj = wfzGjjjtentry.getFgrgjj();//个人公积金
            String fjsfs = wfzGjjjtentry.getFjsfs();//组织间结算方式
            String fcbzxxz = wfzGjjjtentry.getFcbzxxz();//成本中心属性
            Long fisdy = wfzGjjjtentry.getFisdy();//是否当月缴纳
            Long fislz = wfzGjjjtentry.getFislz();//是否离职员工
            String fvourtype1 = wfzGjjjtentry.getFvourtype();//凭证字

            GjjjtEntryInfo gjjjtEntryInfo = new GjjjtEntryInfo();
            gjjjtEntryInfo.setCostCenter(fcostnumber);
            gjjjtEntryInfo.setCostcompany(fcostcompany);
            gjjjtEntryInfo.setGjj(fgjj);
            gjjjtEntryInfo.setGrgjj(fgrgjj);
            gjjjtEntryInfo.setJsfs(fjsfs);
            gjjjtEntryInfo.setCbzxxz(fcbzxxz);
            gjjjtEntryInfo.setIsdy(fisdy);
            gjjjtEntryInfo.setIslz(fislz);
            gjjjtEntryInfo.setVourType(fvourtype1);

            entry.add(gjjjtEntryInfo);
        }
        gjjjtInfo.setEntry(entry);

        //转为JSon
        String json = JSONObject.toJSONString(gjjjtInfo);
        return json;
    }
}
